package org.healthy.util;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;


import org.healthy.model.page.Pagination;
import org.healthy.solr.bean.SolrPagination;

import java.io.IOException;
import java.util.List;

public class SolrJOper<T> {
    String serverUrl = "http://123.57.205.140:8983/solr/collection1";
    String societyUrl="http://123.57.205.140:8983/solr/society";

    /**
     * 根据关键字查询
     * @param <T>
     * @param keyword  搜索关键字
     * @param pageNum  当前页码
     * @param pageSize 每页显示的大小
     * @param clzz     对象类型
     * @return
     */
    public <T> Pagination<T> queryBean(SolrServer server,String keyword, int pageNum, int pageSize, Class<T> clzz) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.setStart((pageNum - 1) * pageSize);
        query.setRows(pageSize);
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (SolrServerException e) {
            e.printStackTrace();
            return null;
        }

        //查询到的记录总数
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue();
        //查询结果集
        List<T> items = response.getBeans(clzz);
        //填充page对象
        return new Pagination<T>(pageNum, pageSize, items);
    }

    /**
     * 根据关键字查询 [测试通过 - 使用 solr内部转换机制]
     *
     * @param <T>
     * @param keyword  搜索关键字
     * @param pageNum  当前页码
     * @param pageSize 每页显示的大小
     * @param clzz     对象类型
     * @return
     */
    public <T> Pagination<T> queryBinderBean(SolrServer server,String keyword, int pageNum, int pageSize, Class<T> clzz) {
        SolrQuery query = new SolrQuery();
        query.setQuery(keyword);
        query.setStart((pageNum - 1) * pageSize);
        query.setRows(pageSize);
        QueryResponse response = null;
        try {
            response = server.query(query);
        } catch (SolrServerException e) {
            e.printStackTrace();
            return null;
        }
        //查询结果集
        SolrDocumentList documents = response.getResults();

        //使用DocumentObjectBinder获取
        List<T> items = server.getBinder().getBeans(clzz, documents);

        //查询到的记录总数
        long totalRow = Long.valueOf(response.getResults().getNumFound()).intValue();
        //填充page对象
        return new Pagination<T>(pageNum, pageSize, items);
    }


    /**
     * 获取solrServer
     *
     * @return
     */
    public SolrServer getSolrServer(String url) {
        HttpSolrServer solr = new HttpSolrServer(url);
        solr.setConnectionTimeout(100);
        solr.setDefaultMaxConnectionsPerHost(100);
        solr.setMaxTotalConnections(100);
        solr.setMaxTotalConnections(100);
        return solr;
    }

/**
    public void search() throws Exception {


        SolrServer solrServer = new HttpSolrServer(serverUrl);
        SolrQuery solrQuery = new SolrQuery("*:*");
        solrQuery.set("qt", "/query");
        solrQuery.setStart(0);
        solrQuery.setRows(Integer.MAX_VALUE);

        QueryResponse resp = solrServer.query(solrQuery);

        SolrDocumentList hits = resp.getResults();
        List<Parameter> li = resp.getBeans(Parameter.class);
        System.out.println(li.size() + "---��doc");
        for (SolrDocument doc : hits) {
            for (String fieldName : doc.getFieldNames()) {
                System.out.println(fieldName + " : " + doc.getFieldValue(fieldName) + "  ");
            }
        }

    }*/

    public void addDoc(SolrServer solrServer) {

        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "004");
        doc.addField("category", "131321");
        doc.addField("word", "13212313");

        try {
            solrServer.add(doc);
            solrServer.optimize();
            solrServer.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void addBean(SolrServer solrServer,T o)
    {
        try {
            solrServer.addBean(o);
            solrServer.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }

    }


}  
